// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import {SwimShopData} from "../web/model/SwimModel";
import ItemFrame from "../item/itemFrame";
import {model} from "../web/model/Model";
import {ResType} from "../config/config";
import {COMMON_CID} from "../web/model/UserModel";
import executionOrder = cc._decorator.executionOrder;

const {ccclass, property} = cc._decorator;

@ccclass
@executionOrder(-1)
export default class ConfirmConsume extends cc.Component {

  @property(cc.Node)
  itemFrame1: cc.Node = null;

  @property(cc.Node)
  itemFrame2: cc.Node = null;

  private _itemPieceScript: ItemFrame = null;
  private _powerfulPieceScript: ItemFrame = null;

  private data: SwimShopData = null;

  onLoad() {
    this._powerfulPieceScript = this.itemFrame2.getComponent(ItemFrame)
  }

  setMergeData(data: SwimShopData) {
    this.data = data
    this.itemPieceScript.setItemData(data.id, model.user.getItemNum(data.fragmentId), ResType.skinPiece)
    const consumePowerfulPiece = data.mergeCount - model.user.getItemNum(data.fragmentId)
    this.powerfulPieceScript.setItemData(COMMON_CID.PIECE_POWERFUL, consumePowerfulPiece)
  }

  onMerge() {
    this.node.emit('swim-merge', this.data.id)
    this.node.active = false
  }

  onCancel() {
    this.node.active = false;
  }

  onClose() {
    this.node.active = false
  }

  get itemPieceScript(): ItemFrame {
    if(!this._itemPieceScript) {
      this._itemPieceScript = this.itemFrame1.getComponent(ItemFrame)
    }
    return this._itemPieceScript;
  }

  get powerfulPieceScript(): ItemFrame {
    if(!this._powerfulPieceScript) {
      this._powerfulPieceScript = this.itemFrame2.getComponent(ItemFrame)
    }
    return this._powerfulPieceScript;
  }

}
